#!/usr/bin/perl -w # for syntax hilighting

use gri_database;
use Data::Dumper;

sub mm_form_docrepos_admin_modify_group_populate {
my ($group,$group_description,$group_users)=@_;

	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $DB=new gri_database("$db_dir/gri.db");
	$group="" if $group eq "?group - not defined?";
	$group_description="" if $group_description eq "?group_description - not defined?";
	$group_users="" if $group_users eq "?group_users - not defined?";
	
	# If we have no details we need to get details from the database using
	# the section name.

	$group=hex_to_string($group);
	if($group_users eq "") {
		my ($rc,$info)=$DB->get_group(group => $group);
		if($rc!=1) {
			return "Error: Group '$group' is not defined.";
		}
		# name,parent,description,title,reader_groups,publisher_groups
		$group_description=$info->{description};
		$group_users=$info->{users};
	} else {
		$group_description=hex_to_string($group_description);
		$group_users=hex_to_string($group_users);
	}
my $s="
    <tr class=gri_headerrow>
      <td class=gri_cell colspan=3>Existing Document Repository Group Details</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Name:</font></td>
      <td class=gri_cell>&nbsp;&nbsp;&nbsp;</td>
      <td class=gri_cell>
        <INPUT TYPE=\"text\" VALUE=\"$group\" NAME=\"name\" SIZE=\"60\" DISABLED MAXLENGTH=\"40\" style=\"font-family:Arial; font-size:11px; background-color: #FFFFFF; border:1px double #E0E0E0;\">
        <input type=hidden name=group value=\"$group\"/>
      </td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Description:</font></td>
      <td class=gri_cell>&nbsp;&nbsp;&nbsp;</td>
      <td class=gri_cell>
        <TEXTAREA NAME=\"group_description\" COLS=60 ROWS=5 style=\"font-family:Arial; font-size:11px; background-color: #FFFFFF; border:1px double #E0E0E0;\">$group_description</TEXTAREA>
      </td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Users</font></td>
      <td class=gri_cell>&nbsp;&nbsp;&nbsp;</td>
      <td class=gri_cell>\n";

	# Get a list of valid users from the authd process
	my $users=$::GRI_FRONTEND->query_authd("GET_USER_LIST");
	my @GU=split(/,/,$group_users);
	if(!defined($users)) {
		print STDERR "Error: mm_form_docrepos_admin_modify_group_populate :: GET_USER_LIST request errored!\n";
		return "Error: mm_form_docrepos_admin_modify_group_populate :: GET_USER_LIST errored!";
	} 
	
	my $row=0; my $col=0;
	$s.="<table cols=4 width=100%>\n";
	
	my @L=split(/\n/,$users);
	@L=sort {my @a2=split(/\|/,$a); my @b2=split(/\|/,$b); $a2[1] cmp $b2[1]} @L[1 .. $#L];
	for my $crep (@L) {
		 my ($uid,$name)=split(/\|/,$crep);
		# Assume authd filters out users we do not want
		 if($col==0) {
		 	if($row!=0) {
				$s.="</tr>\n";
			}
			$s.="<tr>\n";
		}
		$s.="<td align=left valign=left style=\"font-family:arial; font-size: 80%\">";
		if(grep {$uid eq $_} @GU) {
			$s.="<input type=checkbox name=\"user_$uid\" value=1 checked=yes>$name [$uid]<br>";
		 } else {
			$s.="<input type=checkbox name=\"user_$uid\" value=1>$name [$uid]<br>";
		 }
		 $s.="</td>\n";
		 $col++;
		 if($col>3) {
		 	$col=0;
			$row++;
		}
	}
	$s.="\n</td></tr>
    <tr>
      <td colspan=3 align=left><font class=stdtextsmall>&nbsp;</font></td>
    </tr>\n</td></tr>\n";
	return $s;
}

1;

# vim: ts=4:sw=4
